package com.kavysoft.prueba;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ImageAdapter extends ArrayAdapter<ItemGrid> {
    private Context mContext;
    int layoutResourceId;
    ArrayList<ItemGrid> data = new ArrayList<ItemGrid>();
    ItemGrid item;
    int posicion;
	RecordHolderG holder;
	MainActivity main;
	
	public ImageAdapter(Context context, int layoutResourceId,ArrayList<ItemGrid> data, MainActivity main) {
		
		super(context, layoutResourceId,data);
		this.layoutResourceId = layoutResourceId;
		this.mContext = context;
		this.data = data;
		this.main = main;
	}
    
    @Override
	public int getPosition(ItemGrid item) {
		// TODO Auto-generated method stub
		return super.getPosition(item);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return super.getItemId(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		
		holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new RecordHolderG();
			holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
			holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
			holder.imageButton = (ImageButton) row.findViewById(R.id.item_imagebutton);
			row.setTag(holder);
		} else {
			holder = (RecordHolderG) row.getTag();
		}

		item = data.get(position);
		holder.txtTitle.setText(item.getTitle());
		this.posicion = position;
		
		if(item.getSw()){
			holder.imageItem.setImageBitmap(item.getImage_on());
		}else{
			holder.imageItem.setImageBitmap(item.getImage_off());
		}
		
		ButtonClick listener = new ButtonClick(data, position, this, main, item.getMessage());
		
		holder.imageButton.setOnClickListener(listener);
		
		return row;

	}

	static class RecordHolderG {
		TextView txtTitle;
		ImageView imageItem;
		ImageButton imageButton;

	}
}

